<?php
    header('Content-Type: text/html; charset=UTF-8');
    include_once '../../config.php';

    //Variablen
    $p_nr         = $_POST['p_nr'];
    $p_clientid   = $_POST['p_client_id'];
    if (empty($_POST['p_date_offer'])) {
        $p_dateoffer = date('Y-m-d');
    } else {
        $p_dateoffer = $_POST['p_date_offer'];
    }
    $p_offersum   = $_POST['p_offer_sum'];
    $p_km         = $_POST['p_km_list'];
    $statuschoose = $_POST['status_choose'];
    $p_comment    = $_POST['p_comment'];

    $tempname = $_FILES['file']['tmp_name'];
    $name = $_FILES['file']['name'];
    $error = $_FILES['file']['error'];
    $size = $_FILES['file']['size'];
    $pfad = '../../assets/uploads';
    $rights = "0777";
    if ($_FILES['file']['error'] == UPLOAD_ERR_OK) {
        $sql = "INSERT INTO file (pid,file,alias,size) VALUES('" . $p_nr . "','" . $name . "','" . $_POST['alias'] . "','" . $size . "')";
        mysql_query($sql);
        $handle = is_dir($pfad.'/'.$p_nr);
        if (!$handle) {
            mkdir($pfad.'/'.$p_nr);
        }
        move_uploaded_file($tempname, '../../assets/uploads/' . $p_nr . '/' . $name);
    }

    if ($p_km == 'no') {
        header('Location: ../edit.php?project_nr=' . $p_nr .'&client_id=' . $p_clientid . '&km=no');
    } else if ($p_km == 'new') {
        $km_name      = $_POST['km_name'];
        $km_firstname = $_POST['km_vorname'];
        $km_phone     = $_POST['km_phone'];
        $km_email     = $_POST['km_email'];
        $km_mobil     = $_POST['km_mobil'];

        if ($km_name == '') {
            header('Location: ../edit.php?project_nr=' . $p_nr .'&client_id=' . $p_clientid . '&km=name');
        } else {
            if ($statuschoose == 0) {
                $sql = "INSERT INTO project_manager (client_id,name,vorname,phone,email,mobil,km_or_pm)"
                     . " VALUES ('" . $p_clientid . "','" . $km_name . "','" . $km_firstname . "','" . $km_phone
                     . "','" . $km_email . "','" . $km_mobil . "','km')";
                mysql_query($sql) or die(mysql_error());
                $sql = "UPDATE project SET date_offer = '" . $p_dateoffer . "', offer_sum = '" . $p_offersum . "', km_id = '" . mysql_insert_id()
                     . "', comment = '" . $p_comment . "', get = 0 WHERE project_nr = '" . $p_nr . "'";
                mysql_query($sql) or die(mysql_error());

                header('Location: ../index.php');
            } else if ($statuschoose == 1) {
                $sql = "INSERT INTO project_manager (client_id,name,vorname,phone,email,mobil,km_or_pm)"
                     . " VALUES ('" . $p_clientid . "','" . $km_name . "','" . $km_firstname . "','" . $km_phone
                     . "','" . $km_email . "','" . $km_mobil . "','km')";
                mysql_query($sql) or die(mysql_error());
                $sql = "UPDATE project SET date_offer = '" . $p_dateoffer . "', offer_sum = '" . $p_offersum . "', km_id = '" . mysql_insert_id()
                     . "', comment = '" . $p_comment . "', get = 1 WHERE project_nr = '" . $p_nr . "'";
                mysql_query($sql) or die(mysql_error());

                header('Location: ../edit.php?project_nr=' . $p_nr .'&client_id=' . $p_clientid .'');
            } else if ($statuschoose == 2) {
                $sql = "INSERT INTO project_manager (client_id,name,vorname,phone,email,mobil,km_or_pm)"
                     . " VALUES ('" . $p_clientid . "','" . $km_name . "','" . $km_firstname . "','" . $km_phone
                     . "','" . $km_email . "','" . $km_mobil . "','km')";
                mysql_query($sql) or die(mysql_error());
                $sql = "UPDATE project SET date_offer = '" . $p_dateoffer . "', offer_sum = '" . $p_offersum . "', km_id = '" . mysql_insert_id()
                     . "', comment = '" . $p_comment . "', get = 2 WHERE project_nr = '" . $p_nr . "'";
                mysql_query($sql) or die(mysql_error());

                header('Location: ../index.php');
            } else {
                $sql = "INSERT INTO project_manager (client_id,name,vorname,phone,email,mobil,km_or_pm)"
                     . " VALUES ('" . $p_clientid . "','" . $km_name . "','" . $km_firstname . "','" . $km_phone
                     . "','" . $km_email . "','" . $km_mobil . "','km')";
                mysql_query($sql) or die(mysql_error());
                $sql = "UPDATE project SET date_offer = '" . $p_dateoffer . "', offer_sum = '" . $p_offersum . "', km_id = '" . mysql_insert_id()
                     . "', comment = '" . $p_comment . "', get = 4 WHERE project_nr = '" . $p_nr . "'";
                mysql_query($sql) or die(mysql_error());

                header('Location: ../index.php');
            }
        }
    } else {
        if ($statuschoose == 0) {
            $sql = "UPDATE project SET date_offer = '" . $p_dateoffer . "', offer_sum = '" . $p_offersum . "', km_id = '" . $p_km
                 . "', comment = '" . $p_comment . "', get = 0 WHERE project_nr = '" . $p_nr . "'";
            mysql_query($sql) or die(mysql_error());

            header('Location: ../index.php');
        } else if ($statuschoose == 1) {
            $sql = "UPDATE project SET date_offer = '" . $p_dateoffer . "', offer_sum = '" . $p_offersum . "', km_id = '" . $p_km
                 . "', comment = '" . $p_comment . "', get = 1 WHERE project_nr = '" . $p_nr . "'";
            mysql_query($sql) or die(mysql_error());

            header('Location: ../edit.php?project_nr=' . $p_nr .'&client_id=' . $p_clientid .'');
        } else if ($statuschoose == 2) {
            $sql = "UPDATE project SET date_offer = '" . $p_dateoffer . "', offer_sum = '" . $p_offersum . "', km_id = '" . $p_km
                 . "', comment = '" . $p_comment . "', get = 2 WHERE project_nr = '" . $p_nr . "'";
            mysql_query($sql) or die(mysql_error());

            header('Location: ../index.php');
        } else {
            $sql = "UPDATE project SET date_offer = '" . $p_dateoffer . "', offer_sum = '" . $p_offersum . "', km_id = '" . $p_km
                 . "', comment = '" . $p_comment . "', get = 4 WHERE project_nr = '" . $p_nr . "'";
            mysql_query($sql) or die(mysql_error());

            header('Location: ../index.php');
        }
    }



?>
